package com.example.repository;

import com.example.domain.Clazz;
import com.example.domain.Course;
import com.example.domain.Schedule;
import com.example.domain.Teacher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;


public interface ScheduleRepository extends PagingAndSortingRepository<Schedule, Integer>, JpaSpecificationExecutor {
    
    
    Page<Schedule> findAllByClazz(Clazz clazz, Pageable pageable);
    
    Page<Schedule> findAllByCourse(Course course, Pageable pageable);
    
    Page<Schedule> findAllByClazzAndCourse(Clazz clazz, Course course, Pageable pageable);
    
    @Query(value = "select * from t_schedule  where clazz_id = :clazzid and course_id = :courseid and teacher_id " +
            "= :teacherid",
            nativeQuery = true)
    Page<Schedule> findAllByClazzIdAndCourseIdAndTeacherId(@Param(value = "clazzid") int clazzid,
                                                           @Param(value = "courseid") int courseid,
                                                           @Param(value = "teacherid") int teacherid,
                                                           Pageable pageable);
    
    @Query(value = "select * from t_schedule  where clazz_id = :clazzid and course_id = :courseid ",
            nativeQuery = true)
    Page<Schedule> findAllByClazzIdAndCourseId(@Param(value = "clazzid") int clazzid,
                                               @Param(value = "courseid") int courseid,
                                               Pageable pageable);
    
    Page<Schedule> findAllByClazzAndTeacher(Clazz clazz, Teacher teacher, Pageable pageable);
    
    Page<Schedule> findAllByCourseAndTeacher(Course course, Teacher teacher, Pageable pageable);
    
    Iterable<Schedule> findByTeacherIsNull();
    
    Iterable<Schedule> findByTeacherNotNull();
    
    Iterable<Schedule> findAllByTeacherId(int id);
    
    Iterable<Schedule> findAllByClazz_Id(Iterable<Integer> clazzids);
    
    @Query(value = "select * from t_schedule  where teacher_id = :teacherid ",
            nativeQuery = true)
    Page<Schedule> findAllByTeacherId(@Param(value = "teacherid") int id, Pageable pageable);
    
    @Query(value = "select * from t_schedule  where clazz_id = :clazzid and teacher_id = :teacherid ",
            nativeQuery = true)
    Page<Schedule> findAllByClazzIdAndTeacherId(@Param(value = "clazzid") int clazzid, @Param(value = "teacherid") int teacherid, Pageable pageable);
    
    @Query(value = "select * from t_schedule  where clazz_id = :clazzid  ",
            nativeQuery = true)
    Page<Schedule> findAllByClazzId(@Param(value = "clazzid") int clazzid, Pageable pageable);
    
    
    @Query(value = "select * from t_schedule  where course_id = :courseid and teacher_id = :teacherid ",
            nativeQuery = true)
    Page<Schedule> findAllByCourseIdAndTeacherId(@Param(value = "courseid") int courseid, @Param(value = "teacherid") int teacherid, Pageable pageable);
    
    
    @Query(value = "select * from t_schedule  where course_id = :courseid ",
            nativeQuery = true)
    Page<Schedule> findAllByCourseId(@Param(value = "courseid") int courseid, Pageable pageable);
    
    
    
    void deleteAllByClazz_IdIn(Iterable<Integer> clazzids);
    
    Iterable<Schedule> findAllByClazz_IdIn(Iterable<Integer> clazzids);
}
